package com.springboot.swagger2.controller;

import com.springboot.swagger2.entity.User;
import com.springboot.swagger2.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @Desc:
 * @Date: 2020-07-12
 * @Version:v1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户列表", notes = "用户列表")
    @RequestMapping(value = {""}, method = RequestMethod.GET)
    public List<User> getUsers() {
        return userService.findAll();
    }

    @ApiOperation(value = "创建用户", notes = "创建用户")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public User saveUser(@RequestBody User user) {
        return userService.saveUser(user);
    }

    @ApiOperation(value = "获取用户信息", notes = "根据url的id来获取详细新")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public User findUserById(@PathVariable int id) {
        return userService.findUserById(id);
    }

    @ApiOperation(value = "更新用户", notes = "根据url的id来指定更新用户信息")
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public User updateUser(@PathVariable("id") int id, @RequestBody User user) {
        user.setId(id);
        return userService.updateUser(user);
    }

    @ApiOperation(value = "删除用户", notes = "根据url的id来指定删除用户")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public int deleteUserById(@PathVariable("id") int id) {
        return userService.deleteUserById(id);
    }

    @ApiIgnore // 使用该注解忽略这个api
    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    public String jsonTest() {
        return "hello world, springboot-swagger2";
    }
}
